<template>
    <div class="layui-input-<%= viewType %>">
        <% checkboxList.forEach(item => { %>
        <input type="checkbox" name="<%= item.name %>" title="<%= item.title %>" value="<%= item.value %>"
            <% if(skin){ %>
            lay-skin="<%= skin %>"
            <% } %>
            <% if(item.checked){ %>
            checked
            <% } %>
            <% if(item.text){ %>
            lay-text="<%= item.text %>"
            <% } %>
            <% if(item.disabled){ %>
            disabled
            <% } %>
            <% if (eventMethod) { %>
            lay-filter="input-checkbox-<%= templateId %>"
            <% } %>
        >
        <% }); %>
    </div>
</template>
<script name="compile">
    function templateCompile(data) {
        var defaultData = {
            type: "module",
            placeholder: "请选择",
            verify: false,
            viewType: "inline",
            staticData:true,
            skin:false,
            eventMethod: false
            //选择列表
            // checkboxList:[]
        };
        var newData = Object.assign(defaultData, data);
        var callback = {
            success: true,
            result: newData
        };
        if (!newData.checkboxList) {
            return {
                success: false,
                result: "选择器模块缺少参数：checkboxList"
            };
        }
        if(newData.staticData){
            if(newData.checkboxList instanceof Array) {
                if (newData.checkboxList.length <= 0) {
                    return {
                        success: false,
                        result: "选择器模块参数：checkboxList数组长度必须大于1"
                    };
                } else {
                    for (let item of newData.checkboxList) {
                        if (item.name === undefined || item.name === '') {
                            return {
                                success: false,
                                result: "选择器模块参数：checkboxList数组里的值缺少参数name"
                            };
                        }  else if (item.title === undefined || item.title === '') {
                            return {
                                success: false,
                                result: "选择器模块参数：checkboxList数组里的值缺少参数title"
                            };
                        } else if (item.value === undefined || item.name === '') {
                            return {
                                success: false,
                                result: "选择器模块参数：checkboxList数组里的值缺少参数value"
                            };
                        }
                    }
                }
            }else{
                return {
                    success: false,
                    result: "选择器模块参数staticData为true时checkboxList必须为数组"
                };
            }
        }else {
            if (!newData.checkboxList instanceof String) {
                return {
                    success: false,
                    result: "选择器模块参数staticData为true时checkboxList必须为字符串"
                };
            }
        }
        if (newData.eventMethod) {
            if(!newData.eventMethod instanceof String){
                return {
                    success: false,
                    result: "选择器模块参数：eventMethod必须为字符串"
                };
            }else if(newData.templateId){
                return {
                    success: false,
                    result: "多选框拥有eventMethod参数时templateId必须传"
                };
            }
        }
        return callback;
    }
</script>
<script name="templateScript">
    <% if (eventMethod) { %>
        form.on('checkbox(input-checkbox-<%= templateId %>)', function (data) {
            <%- eventMethod %>
        });
    <% } %>
</script>